package com.zjdiepu.www.task.message;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.zjdiepu.www.base.enums.type.ModeIDType;
import com.zjdiepu.www.biz.helper.ServiceHelper;
import com.zjdiepu.www.common.util.DateUtil;
import com.zjdiepu.www.ibiz.other.IOtherWantWarningService;
import com.zjdiepu.www.ibiz.zpp.SystemService;
import com.zjdiepu.www.model.other.dto.OtherWantWarning;
import com.zjdiepu.www.model.other.ext.OtherWantWarningExt;
/**
 * 项目名称：diepu-parent-task <br/> 
 * 类名称：MessageTask <br/>  
 * 类描述： 消息预警任务<br/>    
 * 创建人：xiongxiaotun <br/> 
 * 创建时间：2017年6月2日 下午1:45:43  <br/>   
 * 修改人：xiongxiaotun <br/> 
 * 修改时间：2017年6月2日 下午1:45:43   <br/>  
 * 修改备注：    <br/> 
 * @version V1.0   <br/>
 */
@Component
public class MessageTask {
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Resource
	private IOtherWantWarningService otherWantWarningService;
	@Autowired
	private ServiceHelper serviceHelper;
	@Autowired
	private SystemService systemService;
	/**
	 * @author xiongxiaotun <br/>
	 * @Title: tcAlert  <br/>
	 * @Description: 提车预警 <br/>
	 * @version V1.0  <br/>
	 */
	@Scheduled(cron="${schedule.tc.alert.cron}")
	public void tcAlert() {
		logger.debug("tc alert start.");
		OtherWantWarningExt otherWantWarningExt = new OtherWantWarningExt();
		otherWantWarningExt.setBaseWhere();
		Date now = new Date();
		otherWantWarningExt.setTimeWarningEnd(DateUtil.truncAndAddDays(now, 1)); //截止今天需要预警的
		otherWantWarningExt.setModeId(ModeIDType.TC.getCode());
		
		sendAlertMsg(otherWantWarningExt,"发送提车预警消息失败，请核查，预警ID：");
		logger.debug("tc alert end.");
	}
	/**
	 * @author xiongxiaotun <br/>
	 * @Title: spAlert  <br/>
	 * @Description: 上牌牌预警 <br/>
	 * @version V1.0  <br/>
	 */
	@Scheduled(cron="${schedule.sp.alert.cron}")
	public void spAlert() {
		logger.debug("sp alert start.");
		OtherWantWarningExt otherWantWarningExt = new OtherWantWarningExt();
		otherWantWarningExt.setBaseWhere();
		Date now = new Date();
		otherWantWarningExt.setTimeWarningEnd(DateUtil.truncAndAddDays(now, 1)); //截止今天需要预警的
		otherWantWarningExt.setModeId(ModeIDType.SP.getCode());
		
		sendAlertMsg(otherWantWarningExt,"发送上牌预警消息失败，请核查，预警ID：");
		logger.debug("sp alert end.");
	}
	
	/**
	 * @author xiongxiaotun <br/>
	 * @Title: dyAlert  <br/>
	 * @Description: 抵押牌预警 <br/>
	 * @version V1.0  <br/>
	 */
	@Scheduled(cron="${schedule.dy.alert.cron}")
	public void dyAlert() {
		logger.debug("dy alert start.");
		OtherWantWarningExt otherWantWarningExt = new OtherWantWarningExt();
		otherWantWarningExt.setBaseWhere();
		Date now = new Date();
		otherWantWarningExt.setTimeWarningEnd(DateUtil.truncAndAddDays(now, 1)); //截止今天需要预警的
		otherWantWarningExt.setModeId(ModeIDType.DY.getCode());
		
		sendAlertMsg(otherWantWarningExt,"发送抵押预警消息失败，请核查，预警ID：");
		logger.debug("dy alert end.");
	}
	
	/**
	 * @author xiongxiaotun <br/>
	 * @Title: sqddzAlert  <br/>
	 * @Description: 申请垫资预警 <br/>
	 * @version V1.0  <br/>
	 */
	@Scheduled(cron="${schedule.sqddz.alert.cron}")
	public void sqddzAlert() {
		logger.debug("sqddz alert start.");
		OtherWantWarningExt otherWantWarningExt = new OtherWantWarningExt();
		otherWantWarningExt.setBaseWhere();
		Date now = new Date();
		otherWantWarningExt.setTimeWarningEnd(DateUtil.truncAndAddDays(now, 1)); //截止今天需要预警的
		otherWantWarningExt.setModeId(ModeIDType.SQDDZ.getCode());
		
		sendAlertMsg(otherWantWarningExt,"发送申请代垫资预警消息失败，请核查，预警ID：");
		logger.debug("sqddz alert end.");
	}
	
	/**
	 * @author xiongxiaotun <br/>
	 * @Title: cwdkAlert  <br/>
	 * @Description:财务打款预警 <br/>
	 * @version V1.0  <br/>
	 */
	@Scheduled(cron="${schedule.cwdk.alert.cron}")
	public void cwdkAlert() {
		logger.debug("cwdk alert start.");
		OtherWantWarningExt otherWantWarningExt = new OtherWantWarningExt();
		otherWantWarningExt.setBaseWhere();
		Date now = new Date();
		otherWantWarningExt.setTimeWarningEnd(DateUtil.truncAndAddDays(now, 1)); //截止今天需要预警的
		otherWantWarningExt.setModeId(ModeIDType.CWDK.getCode());
		
		sendAlertMsg(otherWantWarningExt,"发送财务打款预警消息失败，请核查，预警ID：");
		logger.debug("cwdk alert end.");
	}
	
	/**
	 * @author xiongxiaotun <br/>
	 * @Title: sendAlertMsg  <br/>
	 * @Description: 发送预警消息  <br/>
	 * @param otherWantWarningExt
	 * @version V1.0  <br/>
	 */
	private void sendAlertMsg(OtherWantWarningExt otherWantWarningExt, String errorMsg){
		boolean continueHandle = true;
		int page = 0;
		final int row = 50;
		otherWantWarningExt.setRows(row);
		while(continueHandle){
			otherWantWarningExt.setPage(++page); //从第一页开始
			List<OtherWantWarning> otherWantWarnings = otherWantWarningService.selectByExt(otherWantWarningExt);
			for(OtherWantWarning otherWantWarning : otherWantWarnings) {
				try {
					String content = otherWantWarning.getContent();
					if(StringUtils.isNotBlank(content)) {
//						Map<String, Object> tcAlertMsgs = (Map<String, Object>) JSON.parse(content);
//						OtherMessageApp otherMessageApp = (OtherMessageApp)tcAlertMsgs.get("alertMsg");
//						String shortMsg = (String)tcAlertMsgs.get("shortMsg");
//						String salesmanId = (String)tcAlertMsgs.get("salesmanId");
//						String customerName = (String)tcAlertMsgs.get("customerName");
//						otherMessageApp.setTimeSend(new Date());
//						otherMessageApp.setStatusMsg(MessageStatus.WEI_TUI_SONG.getCode());
//						serviceHelper.saveMsg(otherMessageApp, salesmanId, customerName, shortMsg, null, logger);
						
						systemService.diepuNewsHandle(null, otherWantWarning.getModeId(), otherWantWarning.getTypeMsgEvent(), otherWantWarning.getBusiId(), null);
						
					}
					otherWantWarningService.deletedReal(otherWantWarning);
				} catch(Exception e) {
					logger.error(errorMsg + otherWantWarning.getId());
				}
			}
			continueHandle = (otherWantWarnings.size() == row);
		}
	}
}
